﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Data;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;
    using Hidistro.ControlPanel.Commodities;
    using JSShop.Model;
    using JSShop.Enum;

    [ParseChildren(true)]
    public class Searchs : HtmlTemplatedWebControl
    {
        private Common_Location common_Location;
        private wnxd_Shopping_LiList GoodsList;
        private wnxd_Pager Pager;
        private HtmlGenericControl divSpread;
        private int categoryId;
        private Literal keyword;
        private Literal namber;
        
        private Hidistro.Entities.Commodities.CategoryInfo category;
        public Searchs()
        {
            
        }
        protected override void AttachChildControls()
        {
            this.common_Location = (Common_Location)this.FindControl("common_Location");
            this.GoodsList = (wnxd_Shopping_LiList)this.FindControl("GoodsList");
            this.Pager = (wnxd_Pager)this.FindControl("pager");
            this.keyword = (Literal)this.FindControl("keyword");
            this.namber = (Literal)this.FindControl("namber");

            if (!this.Page.IsPostBack)
            {
                if (this.category != null)
                {
                    if (this.common_Location != null)
                    {
                        this.common_Location.CateGoryPath = this.category.Path;
                    }
                    this.LoadPageSearch();
                }
                this.Bind();
            }
        }
        private void Bind()
        {
            ProductBrowseQuery productBrowseQuery = this.GetProductBrowseQuery();
            this.keyword.Text = productBrowseQuery.Keywords;
            DbQueryResult browseProductList = new Juse.Interface.ProductBrowser().GetSearchsProductByCategoryId(productBrowseQuery);
            this.GoodsList.DataSource = browseProductList.Data;
            this.GoodsList.DataBind();
            this.Pager.Total = browseProductList.TotalRecords;
            this.namber.Text = browseProductList.TotalRecords.ToString();
            int cid = 0;
            if (this.category != null) cid = this.category.TopCategoryId;

        }
        private ProductBrowseQuery GetProductBrowseQuery()
        {
            int num2;
            ProductBrowseQuery entity = new ProductBrowseQuery();
            if (int.TryParse(this.Page.Request.QueryString["categoryId"], out num2))
            {
                entity.CategoryId = num2;
            }
            if (int.TryParse(this.Page.Request.QueryString["brand"], out num2))
            {
                entity.BrandId = num2;
            }
            if (int.TryParse(this.Page.Request.QueryString["feature"], out num2))
            {
                entity.feature = num2;
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["keywords"]))
            {
                entity.Keywords = DataHelper.CleanSearchString(Globals.HtmlEncode(Globals.UrlDecode(this.Page.Request.QueryString["keywords"])));
            }
            if (int.TryParse(this.Page.Request.QueryString["filter"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.MaxSalePrice = 100;
                            break;
                        }
                    case 2:
                        {
                            entity.MinSalePrice = 100;
                            entity.MaxSalePrice = 200;
                            break;
                        }
                    case 3:
                        {
                            entity.MinSalePrice = 200;
                            entity.MaxSalePrice = 300;
                            break;
                        }
                    case 4:
                        {
                            entity.MinSalePrice = 300;
                            entity.MaxSalePrice = 500;
                            break;
                        }
                    case 5:
                        {
                            entity.MinSalePrice = 500;
                            break;
                        }
                }
            }
            entity.PageIndex = this.Pager.Index;
            entity.PageSize = this.Pager.Size;
            if (int.TryParse(this.Page.Request.QueryString["rank"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 2:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 3:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 4:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 5:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 6:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 7:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 8:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                }
            }
            else
            {
                entity.SortBy = "DisplaySequence";
            }
            Globals.EntityCoding(entity, true);
            return entity;
        }
        private void LoadPageSearch()
        {
            if (!string.IsNullOrEmpty(this.category.MetaKeywords))
            {
                MetaTags.AddMetaKeywords(this.category.MetaKeywords, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(this.category.MetaDescription))
            {
                MetaTags.AddMetaDescription(this.category.MetaDescription, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(this.category.MetaTitle))
            {
                PageTitle.AddSiteNameTitle(this.category.MetaTitle);
            }
            else
            {
                PageTitle.AddSiteNameTitle(this.category.Name);
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-Searchs.html";
            }
            base.OnInit(e);
        }
    }
}

